/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
    ListNode* reverseBetween(ListNode* head, int m, int n) {
        ListNode* pre_head = new ListNode(0);
        pre_head -> next = head;
        ListNode* pre = pre_head;
        for (int i = 0; i < m - 1; i++)
        {
            pre = pre -> next;
        }
        ListNode* first = pre -> next;
        
        for (int j = m; j < n; j++)
        {
            ListNode *second = first -> next;
            first -> next = second -> next;
            second -> next = pre -> next; //之前错写成了 = first
            pre -> next = second;
        }
        return pre_head -> next;
    }
};
